﻿using System.Collections.Generic;
using System.Text;
using SmartContentModule.Helpers;

namespace SmartContentModule.CssModel
{
    internal class CssAnalyser
    {
        /// <summary>
        /// Gets a string that derived form css file content. New string has a version text attached to image url's
        /// </summary>
        /// <param name="content">String representation of a file</param>
        /// <param name="urlInfoList">Urlinfo list in content</param>
        /// <returns>Returns a string for css with versioned image url's</returns>
        internal static string GetModifiedCss(string content, List<CssUrlInfo> urlInfoList)
        {
            if (urlInfoList == null || urlInfoList.Count == 0)
                return content;

            StringBuilder builder = new StringBuilder(content);
            foreach (CssUrlInfo info in urlInfoList)
            {
                string lastModified = IOHelper.GetLastModificationPostfix(info.FilePath);

                string modified = info.ContentUrl;

                if (!string.IsNullOrEmpty(lastModified))
                {
                    if (modified.Contains("?"))
                    {
                        // custom versioning control assumed.
                    }
                    else
                    {
                        modified += "?v" + lastModified;
                    }
                }

                builder = builder.Replace(info.ContentUrl, modified);
            }

            return builder.ToString();
        }

        /// <summary>
        /// Gets Urlinfo list for a css file
        /// </summary>
        /// <param name="filePath">File path</param>
        /// <param name="directory">directory</param>
        /// <returns>A generic list contains CssUrlInfo representation of all image url's in file</returns>
        internal static List<CssUrlInfo> GetCssUrlInfoList(string filePath, string directory)
        {
            if (!IOHelper.FileExists(filePath))
                return null;

            if (IOHelper.IsCssFile(filePath))
            {
                return GetCssUrlInfoListFromContent(IOHelper.ReadFile(filePath), directory);
            }

            return null;
        }

        private static List<CssUrlInfo> GetCssUrlInfoListFromContent(string cssContent, string directory)
        {
            CssParseHelper cssHelper = new CssParseHelper();
            string[] urlList = cssHelper.ParseUrls(cssContent);
            if (urlList == null || urlList.Length == 0)
                return null;

            List<CssUrlInfo> urlInfoList = new List<CssUrlInfo>();
            foreach (string url in urlList)
            {
                string fileUrl = cssHelper.GetUrlFromCssUrl(url, directory);
                CssUrlInfo info = new CssUrlInfo(fileUrl, directory);
                urlInfoList.Add(info);
            }

            return urlInfoList;
        }
    }
}
